home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell⁄THINK C / Init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  7.3 KB  |  239 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:         CShell
  5. ** File:            init.c
  6. ** Some code from:  Traffic Light 2.0 (2.0 version by Keith Rollin)
  7. ** Modified by:     Eric Soldan
  8. **
  9. ** Copyright © 1989-1991 Apple Computer, Inc.
  10. ** All rights reserved.
  11. */
  12.  
  13.  
  14.  
  15. /*****************************************************************************/
  16.  
  17.  
  18.  
  19. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  20. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  21. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  22.  
  23. #ifndef __ERRORS__
  24. #include <Errors.h>
  25. #endif
  26.  
  27. #ifndef __UTILITIES__
  28. #include "Utilities.h"
  29. #endif
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. /* Set up the whole world, including global variables, Toolbox managers, and
  38. ** menus.  We also create our one application window at this time.  Since
  39. ** window storage is non-relocateable, how and when to allocate space for
  40. ** windows is very important so that heap fragmentation does not occur.
  41. */
  42.  
  43. /* The code that used to be part of ForceEnvirons has been moved into this
  44. ** module.  If an error is detected, instead of merely doing an ExitToShell,
  45. ** which leaves the user without much to go on, we call DeathAlert, which puts
  46. ** up a simple alert that just says an error occurred and then calls
  47. ** ExitToShell.  Since there is no other cleanup needed at this point if an
  48. ** error is detected, this form of error-handling is acceptable.  If more
  49. ** sophisticated error recovery is needed, an exception mechanism, such as is
  50. ** provided by Signals, can be used.
  51. */
  52.  
  53.  
  54.  
  55. /*****************************************************************************/
  56.  
  57.  
  58.  
  59. /* NOTE:  The “g” prefix is used to emphasize that a variable is global. */
  60.  
  61. Boolean    gQuitApplication;        /* Set to 0 by Initialize. */
  62.                                 /* Checked by EventLoop. */
  63.  
  64. extern Boolean        gHasAppleEvents;
  65. extern RgnHandle    gCurrentCursorRgn;
  66. extern short        gPrintPage;
  67.  
  68.  
  69. /*****************************************************************************/
  70. /*****************************************************************************/
  71.  
  72.  
  73.  
  74. #pragma segment Initialize
  75. void    Initialize(void)
  76. {
  77.     long        total, contig;
  78.  
  79.     StandardInitialization(1);            /* 1 MoreMasters. */
  80.         
  81.     /* Make sure that the machine has at least 128K ROMs.
  82.     ** If it doesn’t, exit.
  83.     */
  84.     
  85.     if ((gMachineType < gestaltMac512KE) || (!gHasWaitNextEvent))
  86.         DeathAlert(rBadNewsStrings, sWimpyMachine);
  87.     
  88.     /* We used to make a check for memory at this point by examining
  89.     ** ApplLimit, ApplicZone, and StackSpace and comparing that to the minimum
  90.     ** size we told MultiFinder we needed.  This did not work well because it
  91.     ** assumed too much about the relationship between what we asked
  92.     ** MultiFinder for and what we would actually get back, as well as how to
  93.     ** measure it.  Instead, we will use an alternate method comprised of
  94.     ** two steps.
  95.     */
  96.      
  97.     /* It is better to first check the size of the application heap against a
  98.     ** value that you have determined is the smallest heap the application can
  99.     ** reasonably work in.  This number should be derived by examining the
  100.     ** size of the heap that is actually provided by MultiFinder when the
  101.     ** minimum size requested is used.  The derivation of the minimum size
  102.     ** requested from MultiFinder is described in CShell.h.  The check should
  103.     ** be made because the preferred size can end up being set smaller than
  104.     ** the minimum size by the user.  This extra check acts to ensure that
  105.     ** your application is starting from a solid memory foundation.
  106.     */
  107.      
  108.     if ((long) GetApplLimit() - (long) ApplicZone() < kMinHeap)
  109.         DeathAlert(rBadNewsStrings, sHeapTooSmall);
  110.     
  111.     /* Next, make sure that enough memory is free for your application to run.
  112.     ** It is possible for a situation to arise where the heap may have been of
  113.     ** required size, but a large scrap was loaded which left too little
  114.     ** memory.  To check for this, call PurgeSpace and compare the result with
  115.     ** a value that you have determined is the minimum amount of free memory
  116.     ** your application needs at initialization.  This number can be derived
  117.     ** several different ways.  One way that is fairly straightforward is to
  118.     ** run the application in the minimum size configuration as described
  119.     ** previously.  Call PurgeSpace at initialization and examine the value
  120.     ** returned.  However, you should make sure that this result is not being
  121.     ** modified by the scrap’s presence.  You can do that by calling ZeroScrap
  122.     ** before calling PurgeSpace.  Make sure to remove that call before
  123.     ** shipping, though.
  124.     */
  125.     
  126.     /* ZeroScrap(); */
  127.  
  128.     PurgeSpace(&total, &contig);
  129.     if (total < kMinSpace)
  130.         DeathAlert(rBadNewsStrings, sNoFreeRoomInHeap);
  131.  
  132.     /* The extra benefit to waiting until after the Toolbox Managers have been
  133.     ** initialized to check memory is that we can now give the user an alert
  134.     ** to tell him/her what happened.  Although it is possible that the memory
  135.     ** situation could be worsened by displaying an alert, MultiFinder would
  136.     ** gracefully exit the application with an informative alert if memory
  137.     ** became critical.  Here we are acting more in a preventative manner to
  138.     ** avoid future disaster from low-memory problems.
  139.     */
  140.  
  141.     StandardMenuSetup(rMenuBar, mApple);
  142.     AdjustMenus();
  143.  
  144.     InitAppleEvents();
  145.     InitCustomAppleEvents();
  146.  
  147.     gCurrentCursorRgn  = NewRgn();        /* The current cursor region. */
  148.     DoSetCursor(&QD(arrow));
  149.  
  150.     QD(randSeed) = TickCount();
  151.     gQuitApplication = false;    /* We are only starting.  Don't quit now! */
  152. }
  153.  
  154.  
  155.  
  156. /*****************************************************************************/
  157.  
  158.  
  159.  
  160. /* This function handles the documents selected in the finder, either for
  161. ** loading or for printing.  This is only if we don't have AppleEvents.
  162. ** If we have AppleEvents, then this will all be done automatically via
  163. ** those wonderful AppleEvent thingies.
  164. */
  165.  
  166. #pragma segment Initialize
  167. void    StartDocuments(void)
  168. {
  169.     OSErr        err;
  170.     short        i;
  171.     short        whatToDo;
  172.     short        numberOfFiles;
  173.     AppFile        theAppFile;
  174.     FSSpec        fileSpec;
  175.     long        ignoredProcID;
  176.     FileRecHndl    frHndl;
  177.     WindowPtr    docWindow;
  178.     
  179.     err = noErr;
  180.  
  181.     if (!gHasAppleEvents) {
  182.  
  183.         CountAppFiles(&whatToDo, &numberOfFiles);
  184.  
  185.         if (numberOfFiles > 0) {
  186.  
  187.             for (i = 1; (i <= numberOfFiles) && (!err); ++i) {
  188.                 GetAppFiles(i, &theAppFile);
  189.                 ClrAppFiles(i);
  190.                 err = GetWDInfo(theAppFile.vRefNum,
  191.                                 &fileSpec.vRefNum,
  192.                                 &fileSpec.parID,
  193.                                 &ignoredProcID);
  194.  
  195.                 if (err) Alert(rErrorAlert, nil);
  196.  
  197.                 else {
  198.                     BlockMove((Ptr)&theAppFile.fName,
  199.                               (Ptr) &fileSpec.name,
  200.                               theAppFile.fName[0] + 1
  201.                     );
  202.  
  203.                     err = AppOpenDocument(&frHndl, &fileSpec, fsRdWrPerm);
  204.                     if (!err) {
  205.                         gPrintPage = whatToDo;
  206.                             /* Open the window off-screen if we are printing. */
  207.                         if (err = AppNewWindow(frHndl, &docWindow))
  208.                             AppDisposeDocument(frHndl);
  209.                         else {
  210.                             if (gPrintPage) {
  211.                                 err = AppPrintDocument(frHndl, (i == 1), (i == 1));
  212.                                 AppDisposeDocument(frHndl);
  213.                                 DisposeAnyWindow(docWindow);
  214.                             }
  215.                         }
  216.                         gPrintPage = 0;
  217.                     }
  218.                     if ((err) && (err != userCanceledErr)) {
  219.                         Alert(rErrorAlert, nil);
  220.                         break;
  221.                     }
  222.                 }
  223.             }
  224.             if (whatToDo == appPrint) gQuitApplication = true;
  225.         }
  226.         else {
  227.             err = AppNewDocument(&frHndl);
  228.             if (!err)
  229.                 if (err = AppNewWindow(frHndl, nil))
  230.                     AppDisposeDocument(frHndl);
  231.         }
  232.     }
  233.  
  234.     AppPrintDocument(nil, false, false);    /* Clean up after printing, if we did any. */
  235. }
  236.  
  237.  
  238.  
  239.